﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using CloudShot.Core.Types;

namespace CloudShot.Core.Utils
{
	public static class ImageTypeUtils
	{
		public static ImageSaveFormat GetImageType(string value)
		{
            const ImageSaveFormat defaultFormat = ImageSaveFormat.Png;

            if (string.IsNullOrEmpty(value))
                return defaultFormat;

			if (value.Equals(ImageSaveFormat.Jpeg.ToString(), StringComparison.OrdinalIgnoreCase))
				return ImageSaveFormat.Jpeg;

			if (value.Equals(ImageSaveFormat.Jpg.ToString(), StringComparison.OrdinalIgnoreCase))
				return ImageSaveFormat.Jpg;

			if (value.Equals(ImageSaveFormat.Png.ToString(), StringComparison.OrdinalIgnoreCase))
				return ImageSaveFormat.Png;

			if (value.Equals(ImageSaveFormat.Gif.ToString(), StringComparison.OrdinalIgnoreCase))
				return ImageSaveFormat.Gif;

            return defaultFormat;
		}

		public static string GetImageExtension(this ImageSaveFormat value)
		{
			return value.ToString();
		}

		public static ImageFormat GetImageFormat(this ImageSaveFormat value)
		{
			switch (value)
			{
				case ImageSaveFormat.Png:
					return ImageFormat.Png;

				case ImageSaveFormat.Jpg:
				case ImageSaveFormat.Jpeg:
					return ImageFormat.Jpeg;

				case ImageSaveFormat.Gif:
					return ImageFormat.Gif;

				default:
					throw new ArgumentException("Unsupported image format:" + value, "value");
			}
		}

		public static IEnumerable<string> GetImageExtensions(this IEnumerable<ImageSaveFormat> imageTypes)
		{
			return imageTypes.Select(GetImageExtension);
		}
	}
}
